package com.myapp.Activity;

import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.PopupMenu;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.myapp.Activity.AirLink.AirlinkChooseActivity;
import com.myapp.R;
import com.myapp.View.NoScrollViewPager;
import com.myapp.View.SimpleToolbar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {
    LocalActivityManager manager = null;
    @BindView(R.id.toolbar)
    SimpleToolbar toolbar;
    @BindView(R.id.linearLayout1)
    LinearLayout linearLayout1;
    private int offset = 0;// 动画偏移量
    private int currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度
    private ImageView cursor;// 动画图片
    private Context context;
    public static final String INTENT_CHOOSE = "1";


    @BindView(R.id.viewPage)
    NoScrollViewPager viewPage;
    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.ll2)
    LinearLayout ll2;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.ll3)
    LinearLayout ll3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        manager = new LocalActivityManager(this, true);
        manager.dispatchCreate(savedInstanceState);
        ButterKnife.bind(this);
        initTextView();
        initPagerViewer();
        
        toolbar.setLeftIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        toolbar.setRightIcon1ClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(context,AirlinkChooseActivity.class);
//                startActivity(intent);
                showPopupMenu(v);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void showPopupMenu(View view) {
        // View当前PopupMenu显示的相对View的位置
        PopupMenu popupMenu = new PopupMenu(this, view);
        // menu布局
        popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
        // menu的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_airLink:
                        Intent airLink = new Intent(getApplicationContext(), AirlinkChooseActivity.class);
                        airLink.putExtra(INTENT_CHOOSE,0);
                        startActivity(airLink);
                        Toast.makeText(getApplicationContext(),"airLink",Toast.LENGTH_SHORT);
                        break;
                    case R.id.menu_ap:
                        Intent softLink = new Intent(getApplicationContext(),AirlinkChooseActivity.class);
                        softLink.putExtra(INTENT_CHOOSE,1);
                        startActivity(softLink);
                        Toast.makeText(getApplicationContext(),"ap",Toast.LENGTH_SHORT);
                        break;

                }
                Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        // PopupMenu关闭事件
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {
//                Toast.makeText(getApplicationContext(), "关闭PopupMenu", Toast.LENGTH_SHORT).show();
            }
        });

        popupMenu.show();
    }

    private void initTextView() {
        ll1.setOnClickListener(new MyOnClickListener(0));
        ll2.setOnClickListener(new MyOnClickListener(1));
        ll3.setOnClickListener(new MyOnClickListener(2));

    }

    private void initPagerViewer() {
        viewPage.setNoScroll(true);

        final ArrayList<View> list = new ArrayList<View>();

        Intent intent1 = new Intent(this, DeviceListActivity.class);
        list.add(getView("A", intent1));
        Intent intent2 = new Intent(this, MessageCenterActivity.class);
        list.add(getView("B", intent2));
        Intent intent3 = new Intent(this, UserCenterActivity.class);
        list.add(getView("C", intent3));

        viewPage.setAdapter(new MyPagerAdapter(list));
        viewPage.setCurrentItem(0);

        iv1.setBackgroundResource(R.drawable.my_device_blue);
        iv2.setBackgroundResource(R.drawable.message_center_grey);
        iv3.setBackgroundResource(R.drawable.user_center_grey);

        tv1.setTextColor(getResources().getColor(R.color.blue));
        tv2.setTextColor(getResources().getColor(R.color.grey));
        tv3.setTextColor(getResources().getColor(R.color.grey));

//        viewPage.setOnPageChangeListener(new MyOnPageChangeListener());


    }

    /**
     * 图标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;

        }

        @Override
        public void onClick(View v) {
            viewPage.setCurrentItem(index);

            switch (index) {
                case 0:
                    // t1.setBackgroundColor(getResources().getColor(R.color.gray));
                    // t2.setBackgroundColor(getResources().getColor(R.color.white));
                    // t3.setBackgroundColor(getResources().getColor(R.color.white));

                    iv1.setBackgroundResource(R.drawable.my_device_blue);
                    iv2.setBackgroundResource(R.drawable.message_center_grey);
                    iv3.setBackgroundResource(R.drawable.user_center_grey);

                    tv1.setTextColor(getResources().getColor(R.color.blue));
                    tv2.setTextColor(getResources().getColor(R.color.grey));
                    tv3.setTextColor(getResources().getColor(R.color.grey));
                    toolbar.setMyLeftIcon(R.drawable.left_arrow_white);
                    toolbar.setTitle(getString(R.string.bottom_my_device));
                    toolbar.setMyRightIcon1(R.drawable.add_icon);


                    break;

                case 1:
                    // t1.setBackgroundColor(getResources().getColor(R.color.white));
                    // t2.setBackgroundColor(getResources().getColor(R.color.gray));
                    // t3.setBackgroundColor(getResources().getColor(R.color.white));

                    iv1.setBackgroundResource(R.drawable.my_device_grey);
                    iv2.setBackgroundResource(R.drawable.message_center_blue);
                    iv3.setBackgroundResource(R.drawable.user_center_grey);

                    tv1.setTextColor(getResources().getColor(R.color.grey));
                    tv2.setTextColor(getResources().getColor(R.color.blue));
                    tv3.setTextColor(getResources().getColor(R.color.grey));
                    toolbar.setMyLeftIcon(-1);
                    toolbar.setTitle(getString(R.string.bottom_message_center));
                    toolbar.setMyRightIcon1(-1);
                    break;

                case 2:
                    // t1.setBackgroundColor(getResources().getColor(R.color.white));
                    // t2.setBackgroundColor(getResources().getColor(R.color.white));
                    // t3.setBackgroundColor(getResources().getColor(R.color.gray));

                    iv1.setBackgroundResource(R.drawable.my_device_grey);
                    iv2.setBackgroundResource(R.drawable.message_center_grey);
                    iv3.setBackgroundResource(R.drawable.user_center_blue);

                    tv1.setTextColor(getResources().getColor(R.color.grey));
                    tv2.setTextColor(getResources().getColor(R.color.grey));
                    tv3.setTextColor(getResources().getColor(R.color.blue));
                    toolbar.setMyLeftIcon(-1);
                    toolbar.setTitle(getString(R.string.bottom_user_center));
                    toolbar.setMyRightIcon1(-1);
                    break;

                default:
                    break;
            }
        }
    }

    ;


    /**
     * 通过activity获取视图
     *
     * @param id
     * @param intent
     * @return
     */
    @SuppressWarnings("deprecation")
    private View getView(String id, Intent intent) {
        return manager.startActivity(id, intent).getDecorView();
    }

    /**
     * Pager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        List<View> list = new ArrayList<View>();

        public MyPagerAdapter(ArrayList<View> list) {
            this.list = list;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ViewPager pViewPager = ((ViewPager) container);
            pViewPager.removeView(list.get(position));
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ViewPager pViewPager = ((ViewPager) arg0);
            pViewPager.addView(list.get(arg1));
            return list.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

}
